<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateInvoicesTable extends Migration
{

    const TABLE = \App\Models\Invoice\Invoice::TABLE;

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        !Schema::hasTable(self::TABLE) &&
        Schema::create(self::TABLE, function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('parking_lot_id')->default('');
            $table->uuid('related_id')->default('')->comment('开票订单ID');
            $table->string('related_code', 100)->default('')->comment('开票订单单号');
            $table->string('related_type')->default('')->comment('开票订单类型');
            $table->string('paid_code', 100)->default('')->index()->comment('支付单号');
            $table->timestamp('paid_at')->nullable()->comment('支付时间');
            $table->uuid('original_id')->default('')->index()->comment('红冲-开票记录ID');
            $table->string('type', 50)->default('');
            $table->tinyInteger('tag')->default(0);
            $table->string('status', 50)->default('');
            $table->string('check_code', 50)->default('');
            $table->string('cipher_code')->default('');
            $table->string('code', 50)->default('');
            $table->string('number', 50)->default('');
            $table->float('amount')->default(0);
            $table->float('total_amount')->default(0);
            $table->float('tax_amount')->default(0);
            $table->json('ofd_dat')->nullable();
            $table->json('pdf_dat')->nullable();
            $table->json('good_details')->nullable();
            $table->json('qr_code_dat')->nullable();
            $table->json('xml_dat')->nullable();
            $table->string('remark')->default('');
            $table->string('source', 50)->default('');
            $table->uuid('app_user_id')->default('');
            $table->timestamp('triggered_at')->nullable();
            $table->string('err_msg')->default('');
            $table->index(['related_id', 'related_type'], 'idx_invoice_related');
            $table->index(['parking_lot_id', 'paid_at', 'code', 'number', 'triggered_at', 'app_user_id'], 'uni_idx_invoice_book');
            $table->timestamps();
        });
        DB::statement("alter table `".self::TABLE."` comment '开票记录';");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(self::TABLE);
    }
}
